package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TPanelConfig;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**实现TPanel*/
	public class TPanel extends TComponent
	{
		public static var PanelCaptionHeight:Number=20;
		public static var PanelCaptionFontSize:Number=15;
		
		
		private var _FCaptionBar:Sprite;
		private var _FCaption:TextField;
		private var _FContainer:Sprite;
		
		public function TPanel(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function get Caption():String
		{
			return _FCaption.text;
		}

		public function set Caption(value:String):void
		{
			_FCaption.text = value;
		}

		public function PanelConfig():TPanelConfig{
			return _FConfig as TPanelConfig;
		}
		
		override protected function Init():void{
			super.Init();
			
			_FContainer=new Sprite();
			this.addChild(_FContainer);
			
			if(PanelConfig().ShowCaption){
				_FCaptionBar=new Sprite();
				_FCaption=new TextField();
				
				var _FCaptionFormat:TextFormat=new TextFormat('Tahoma',PanelCaptionFontSize,
					ColorUtility.GetColorFromBaseColor(PanelConfig().BaseColor));
				_FCaption.autoSize=TextFieldAutoSize.LEFT;
				_FCaption.width=PanelConfig().Width;
				_FCaption.defaultTextFormat=_FCaptionFormat;
				_FCaption.text=PanelConfig().Caption;
				
				_FCaptionBar.addChild(_FCaption);
				this.addChild(_FCaptionBar);
			}
			
			if(PanelConfig().Components)
				for(var I:int=0,L:int=PanelConfig().Components.length;I<L;I++){
					if(!PanelConfig().Components[I] is DisplayObject) continue;
					_FContainer.addChild(PanelConfig().Components[I] as DisplayObject);
				}
			Paint();
		}
		
		/**主要是绘制Caption背景*/
		protected function Paint():void{
			var LastY:Number=0;
			if(PanelConfig().ShowCaption){
				with(_FCaptionBar.graphics){
					clear();
					beginFill(ColorUtility.GetBackColorFromBaseColor(PanelConfig().BaseColor),0.3);
					drawRect(0,0,PanelConfig().Width,PanelCaptionHeight);
					endFill();
				}
				with(_FCaption){
					x=10;
					y=(_FCaptionBar.height-height)/2;
				}
				LastY+=_FCaptionBar.height;
			}
			with(_FContainer.graphics){
				clear();
				beginFill(PanelConfig().BaseColor,1);
				drawRect(0,0,PanelConfig().Width,PanelConfig().Height-LastY);
				endFill();
			}
			_FContainer.y=LastY;
		}
		
		public function AddComponent(Obj:DisplayObject):DisplayObject{
			return _FContainer.addChild(Obj);
		}
		
		public function RemoveComponent(Obj:DisplayObject):DisplayObject{
			return _FContainer.removeChild(Obj);
		}
		
	}
}